package com.gao.security.autoconfig.verificationcode.handler;



import com.gao.security.verificationcode.common.ArgumentErrorVo;
import com.gao.security.verificationcode.common.BaseVo;
import com.gao.security.verificationcode.common.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;
import java.util.ArrayList;
import java.util.List;


/**
 * @author GaoShuangWen
 * @date 2018-12-18 9:50
 */
@Slf4j
public class BaseExceptionHandler {
    /*@ExceptionHandler(value = Exception.class)
    public BaseVo error(Exception e) {
        log.info("异常出现，请及时查看。");
        e.printStackTrace();
        return BaseVo.builder().code("500").data(e.getMessage()).build();
    }*/

    /**
     * 违反约束异常
     */

    protected BaseVo handleConstraintViolationException(ConstraintViolationException e, HttpServletRequest request) {
        log.info("handleConstraintViolationException start, uri:{}, caused by: ", request.getRequestURI(), e);

        return BaseVo.failure(ResponseCode.PARAM_IS_INVALID, e.getMessage());
    }


    /**
     * 处理验证参数封装错误时异常
     */
    protected BaseVo handleConstraintViolationException(HttpMessageNotReadableException e, HttpServletRequest request) {
        log.info("handleConstraintViolationException start, uri:{}, caused by: ", request.getRequestURI(), e);
        return BaseVo.failure(ResponseCode.PARAM_IS_INVALID, e.getMessage());
    }


    /**
     * 处理使用@Validated注解时，参数绑定错误异常（反400错误码）
     */
    protected BaseVo handleBindException(BindException e, HttpServletRequest request) {
        log.info("handleMethodArgumentNotValidException start, uri:{}, caused by: ", request.getRequestURI(), e);
        //异常处理逻辑,将错误的信息返回给前端
        List<ArgumentErrorVo> argumentErrorVoList = new ArrayList<>();
        e.getBindingResult().getFieldErrors().forEach(fieldError ->
                argumentErrorVoList.add(ArgumentErrorVo.builder()
                        .filed(fieldError.getField())
                        .errorVal(fieldError.getRejectedValue())
                        .errorMsg(fieldError.getDefaultMessage()).build())
        );
        return BaseVo.failure(ResponseCode.PARAM_IS_INVALID, argumentErrorVoList);
    }

    /**
     * 处理通用自定义业务异常
     */
 /*   protected ResponseEntity<BaseVo> handleBusinessException(BusinessException e, HttpServletRequest request) {
        log.info("handleBusinessException start, uri:{}, exception:{}, caused by: {}", request.getRequestURI(), e.getClass(), e.getMessage());

        ExceptionEnum ee = ExceptionEnum.getByEClass(e.getClass());
        if (ee != null) {
            return ResponseEntity
                    .status(ee.getHttpStatus())
                    .body(Response.failure(ee.getResponseCode(), e.getData()));
        }

        return ResponseEntity
                .status(HttpStatus.OK)
                .body(e.getResponseCode() == null ? BaseVo.failure(e.getMessage()) : BaseVo.failure(e.getResponseCode(), e.getData()));
    }*/

    /**
     * 处理运行时系统异常（反500错误码）
     */
    protected BaseVo handleRuntimeException(RuntimeException e, HttpServletRequest request) {
        log.error("handleRuntimeException start, uri:{}, caused by: ", request.getRequestURI(), e);
        //TODO 通过邮件 发送到开发人员，及时处理
        return BaseVo.failure(ResponseCode.SYSTEM_INNER_ERROR);
    }

}
